using System;
using System.Security;
using System.Security.Principal;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CodePlex.Diagnostics
{
    /// <summary>
    /// Provides unit testing for the <see cref="T:CodePlex.Diagnostics.IIdentitySerializer"/> class.
    /// </summary>
    [TestClass]
    public sealed class IIdentitySerializerUnitTest
    {
        #region public IIdentitySerializerUnitTest()

        /// <summary>
        /// Initializes a new instance of the <see cref="T:IIdentitySerializerUnitTest"/> class.    
        /// </summary>
        public IIdentitySerializerUnitTest()
        {

        }

        #endregion

        #region public void IIdentitySerializationUnitTest()

        /// <summary>
        /// Unit test to exercise the serialization and deserialization of instances of types implementing the <see cref="T:System.Security.Principal.IIdentity"/> interface.
        /// </summary>
        [TestMethod]
        public void IIdentitySerializationUnitTest()
        {
            IIdentity original = WindowsIdentity.GetCurrent() as IIdentity;

            // serialize the current Windows identity using the IIdentitySerializer class.
            string soap = IIdentitySerializer.Serialize(original);

            // ensure that the Soap encoded IIdentity is not null or an empty string. 
            Assert.IsFalse(string.IsNullOrEmpty(soap), "Serialized identity is either null or an empty string.");

            IIdentity deserialized = IIdentitySerializer.Deserialize(soap) as IIdentity;

            // ensure that the deserialized IIdentity is not null, if the 'as' cast fails then 'deserialized' will unfortunately be null.
            Assert.IsNotNull(deserialized, "Deserialized IIdentity is null.");

            Assert.AreEqual<string>(original.Name, deserialized.Name, "Serialized IIdentity contains an invalid Name.");
            Assert.AreEqual<string>(original.AuthenticationType, deserialized.AuthenticationType, "Serialized IIdentity contains an invalid AuthenticationType.");

            Assert.AreEqual<bool>(original.IsAuthenticated, deserialized.IsAuthenticated, "Serialized IIdentity contains an invalid IsAuthenticated.");
        }

        #endregion
    }
}
